package com.example.administrator.chinafood;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.administrator.chinafood.Adapter.ForumListView;
import com.example.administrator.chinafood.Adapter.MyListViewAdapter;
import com.example.administrator.chinafood.SQLite.MyDBOpenHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/11/6.
 */

public class FlForum extends Fragment {

    private ListView list;
    private MyListViewAdapter<ForumListView> myListViewAdapter;
    private List<ForumListView> mData1;
    private RadioGroup radioGroup;
    private int checked = R.id.rb_forum_1;
    private Context context;

    private MyDBOpenHelper myDBOpenHelper;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fl_forum,container,false);
        context = view.getContext();
        list = (ListView) view.findViewById(R.id.forum_list);
        radioGroup = (RadioGroup)view.findViewById(R.id.rg_forum);
        checked = radioGroup.getCheckedRadioButtonId();
        myDBOpenHelper = new MyDBOpenHelper(view.getContext(),"ChinaFood.db");
        init();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb_forum_1:
                        checked = R.id.rb_forum_1;
                        setData(1);
                        break;
                    case R.id.rb_forum_2:
                        checked = R.id.rb_forum_2;
                        setData(2);
                        break;
                    case R.id.rb_forum_3:
                        checked = R.id.rb_forum_3;
                        setData(3);
                        break;
                }
            }
        });
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (checked){
                    case R.id.rb_forum_1:
                        check(1,position);
                        break;
                    case R.id.rb_forum_2:
                        check(2,position);
                        break;
                    case R.id.rb_forum_3:
                        check(3,position);
                        break;
                }
            }
        });
        Button btn = (Button)view.findViewById(R.id.forum_btn_add);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(context,MyForum.class));
            }
        });
        return view;
    }
    private void init(){
        mData1 = new ArrayList<ForumListView>();
        setData("select * from forum");
        myListViewAdapter = new MyListViewAdapter<ForumListView>((ArrayList)mData1,R.layout.adapter_listview_forum){
            @Override
            public void bindView(ViewHolder holder, ForumListView obj) {
                holder.setText(R.id.forum_list_title,obj.getTitle());
                holder.setText(R.id.forum_list_author,obj.getAuthor());
                holder.setText(R.id.forum_list_time,obj.getTime());
                holder.setText(R.id.forum_list_num,obj.getNum());
            }
        };
        list.setAdapter(myListViewAdapter);
    }

    private void setData(String sql){
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery(sql,null);
        String title = "";
        String author = "";
        String time = "";
        String num= "";
        String content = "";
        int personid;
        int img;
        if (cursor.getCount() <=0){
            Toast.makeText(context,"当前内容为空",Toast.LENGTH_SHORT).show();
        }else {
            while (cursor.moveToNext()) {
                title = cursor.getString(cursor.getColumnIndex("title"));
                author = cursor.getString(cursor.getColumnIndex("author"));
                time = cursor.getString(cursor.getColumnIndex("time"));
                num = cursor.getString(cursor.getColumnIndex("num"));
                content = cursor.getString(cursor.getColumnIndex("content"));
                img = cursor.getInt(cursor.getColumnIndex("img"));
                personid = cursor.getInt(cursor.getColumnIndex("personid"));
                mData1.add(new ForumListView(title, author, time, num,content,img,personid));
            }
        }
        cursor.close();
        db.close();
    }

    private void selectData(){
        SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from myliuyan where user = 'author3'",null);
        String personid = "";
        if (cursor.getCount() >=1 ) {
            while (cursor.moveToNext()) {
                personid += cursor.getString(cursor.getColumnIndex("personid")) + ",";
            }
            personid = personid.substring(0,personid.length()-1);
        }

        cursor = db.rawQuery("select * from forum where personid in ("+personid+") ",null);
        String title = "";
        String author = "";
        String time = "";
        String num= "";
        String content = "";
        int thepersonid;
        int img;
        if (cursor.getCount() <=0){
            Toast.makeText(context,"当前内容为空",Toast.LENGTH_SHORT).show();
        }else {
            while (cursor.moveToNext()) {
                title = cursor.getString(cursor.getColumnIndex("title"));
                author = cursor.getString(cursor.getColumnIndex("author"));
                time = cursor.getString(cursor.getColumnIndex("time"));
                num = cursor.getString(cursor.getColumnIndex("num"));
                content = cursor.getString(cursor.getColumnIndex("content"));
                img = cursor.getInt(cursor.getColumnIndex("img"));
                thepersonid = cursor.getInt(cursor.getColumnIndex("personid"));
                mData1.add(new ForumListView(title, author, time, num,content,img,thepersonid));
            }
        }
        cursor.close();
        db.close();
    }

    private void setData(int i){
        if (i == 1){
            mData1.clear();
            setData("select * from forum");
        }else if(i == 2){
            mData1.clear();
            selectData();
        }else{
            mData1.clear();
            setData("select * from forum where author = 'author1'");
        }
        myListViewAdapter.notifyDataSetChanged();
    }

    private void check(int i,int p){
        switch (i){
            case 1:
            case 2:
                ForumListView f = mData1.get(p);
                String [] string = {f.getTitle(),f.getAuthor(),f.getTime(),f.getContent(),f.getNum()};
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putStringArray("string",string);
                bundle.putInt("img",f.getImg());
                bundle.putInt("personid",f.getPersonid());
                intent.putExtras(bundle);
                intent.setClass(context,TheForum.class);
                startActivity(intent);
            case 3:
        }
    }
}
